/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import Entity.EventsCustomer;
import dbConnect.jdbcConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class adapterEventCustomer {

    Connection con = null;

    public adapterEventCustomer() throws Exception {
        con = jdbcConnection.getConnection();
    }

    public ArrayList<EventsCustomer> getAllEventsCustomers() {
        try {
            ArrayList<EventsCustomer> listCusEvents = new ArrayList<EventsCustomer>();
            PreparedStatement pr = con.prepareStatement("select EventCustomers.codeEventCustomer,Customers.userCustomer,Customers.fullName,Customers.email,Customers.dob,Events.nameEvent from EventCustomers inner join Customers on  EventCustomers.codeCustomer=Customers.codeCustomer inner join Events on EventCustomers.codeEvent=Events.codeEvent order by EventCustomers.codeEventCustomer desc");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                EventsCustomer ec = new EventsCustomer();
                ec.setCodeEventCustomer(rs.getInt(1));
                ec.setUserCustomer(rs.getString(2));
                ec.setFullName(rs.getString(3));
                ec.setEmail(rs.getString(4));
                ec.setDob(rs.getString(5));
                ec.setNameEvent(rs.getString(6));
                listCusEvents.add(ec);
            }
            return listCusEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ArrayList<EventsCustomer> searchCodeEventCustomers(int code) {
        try {
            ArrayList<EventsCustomer> listCusEvents = new ArrayList<EventsCustomer>();
            PreparedStatement pr = con.prepareStatement("select EventCustomers.codeEventCustomer,Customers.userCustomer,Customers.fullName,Customers.email,Customers.dob,Events.nameEvent,Customers.codeCustomer,Events.codeEvent from EventCustomers inner join Customers on  EventCustomers.codeCustomer=Customers.codeCustomer inner join Events on EventCustomers.codeEvent=Events.codeEvent where EventCustomers.codeEventCustomer=?");
            pr.setInt(1, code);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {

                EventsCustomer ec = new EventsCustomer();
                ec.setCodeEventCustomer(rs.getInt(1));
                ec.setUserCustomer(rs.getString(2));
                ec.setFullName(rs.getString(3));
                ec.setEmail(rs.getString(4));
                ec.setDob(rs.getString(5));
                ec.setNameEvent(rs.getString(6));
                ec.setCodeCustomer(rs.getInt(7));
                ec.setCodeEvent(rs.getInt(8));
                listCusEvents.add(ec);
            }
            return listCusEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    public ArrayList<EventsCustomer> searchNameCustomers(String name) {
        try {
            ArrayList<EventsCustomer> listCusEvents = new ArrayList<EventsCustomer>();
            PreparedStatement pr =con.prepareCall("{call list(?)}"); 
            pr.setString(1, name);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {

                EventsCustomer ec = new EventsCustomer();
                ec.setCodeEventCustomer(rs.getInt(1));
                ec.setUserCustomer(rs.getString(2));
                ec.setFullName(rs.getString(3));
                ec.setEmail(rs.getString(4));
                ec.setDob(rs.getString(5));
                ec.setNameEvent(rs.getString(6));
                ec.setCodeCustomer(rs.getInt(7));
                ec.setCodeEvent(rs.getInt(8));
                ec.setAddress(rs.getString(9));
                ec.setPhoneNumber(rs.getString(10));
                listCusEvents.add(ec);
            }
            return listCusEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
//method insert admin

    public boolean insertEventCustomers(int cus, int event) {
        try {
            PreparedStatement pr = con.prepareStatement("insert into EventCustomers values(?,?)");
            pr.setInt(1, cus);
            pr.setInt(2, event);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method update admin

    public boolean updateEventCustomers(int codeCustomer,int codeEven,int codeCustomerEvent) {
        try {
            PreparedStatement pr = con.prepareStatement("update EventCustomers set codeCustomer=?,codeEvent=? where codeEventCustomer = ?");
            pr.setInt(1, codeCustomer);
            pr.setInt(2, codeEven);
            pr.setInt(3, codeCustomerEvent);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method delete admin

    public boolean deleteEventCustomers(int code) {
        try {
            PreparedStatement pr = con.prepareStatement("delete EventCustomers  where codeEventCustomer=?");
            pr.setInt(1, code);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
